﻿using System;
using System.Data;
using Titan.FIS.Enum;

namespace Titan.FIS.LmsDataSync
{
    /// <summary>
    /// 数据库操作类
    /// </summary>
    public class DataOperator
    {
        /// <summary>
        /// 插入化验指标结果
        /// </summary>
        /// <param name="belong">所属化验仪器</param>
        /// <param name="autoNo">自动编号</param>
        /// <param name="testCode">化验编码</param>
        /// <param name="testIndex">化验指标</param>
        /// <param name="testValue">化验值</param>
        /// <param name="testSampleWeight">试样质量</param>
        /// <param name="testDate">化验时间</param>
        /// <param name="strErrInfo">错误信息</param>
        /// <returns></returns>
        public static bool DoSaveLmsIndexValue(LaboratoryInstrument belong, string autoNo, string testCode, LaboratoryIndex testIndex, string testValue, string testSampleWeight, string testDate, ref string strErrInfo)
        {
            var bResult = false;
            strErrInfo = string.Empty;
            var parms = new[]{    new Titan.DAS.Msg.Common.ProcParam{Name = "P_TestCode",Value = testCode,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_TestIndex",Value =  (int)testIndex,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},  
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_TestValue",Value = testValue,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)}, 
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_AutoNo",Value = autoNo,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_Belong",Value = (int)belong,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_SampleWeight",Value = testSampleWeight,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_TestDate",Value =System.DateTime.Parse(testDate) ,Direction = ParameterDirection.Input,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Date)}, 
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRCODE",Value = 0,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.Decimal)},
                                  new Titan.DAS.Msg.Common.ProcParam{Name = "P_ERRMSG",Value = null,Direction = ParameterDirection.Output,DbType = Convert.ToInt32(Titan.DAS.Client.Data.ODB_Type.NVarchar2),Size=2000}};
            var modelProc = DAS.Client.Data.DataHelper.Instance.ExecProc(Common.UserName, Common.UserPwd, "PCK_LMSDATAINFO.INSERT_LMS_COLLECTIONDATA", parms);
            if (!modelProc.RSuccess || modelProc.RStatuCode != 1)
            {
                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelProc.RStatuCode, modelProc.RStatuDesc + modelProc.RErrInfo);
            }
            else
            {
                strErrInfo = modelProc.RParams[modelProc.RParams.Length - 1].Value == null ? string.Empty : modelProc.RParams[modelProc.RParams.Length - 1].Value.ToString().Replace("null", "").Trim();
                if (!string.IsNullOrEmpty(strErrInfo)) return false;
                bResult = true;
            }
            return bResult;
        }


//        public static bool DoSaveLmsIndexValue(LaboratoryInstrument belong, string autoNo, string testCode, LaboratoryIndex testIndex, string testValue, string testSampleWeight, string testDate, ref string strErrInfo)
//        {
//            var bResult = false;
//            var strSql = string.Format(@"CALL PCK_LMSDATAINFO.INSERT_LMS_COLLECTIONDATA(                                                                              
//                                                                                P_TestCode => '{0}',
//                                                                                P_TestIndex => '{1}',
//                                                                                P_TestValue => {2},
//                                                                                P_AutoNo => '{3}',
//                                                                                P_Belong => {4},
//                                                                                P_SampleWeight => {5},
//                                                                                P_TestDate => TO_DATE('{6}','YYYY-MM-DD HH24:MI:SS')
//                                                                                )",                                
//                testCode,
//                (int)testIndex,
//                testValue,
//                autoNo,
//                (int)belong,
//                testSampleWeight,
//                testDate
//                );
//            var modelSql = Titan.DAS.Client.Data.DataHelper.Instance.ExecSql(Common.UserName, Common.UserPwd, strSql);
//            if (!modelSql.RSuccess)
//            {
//                strErrInfo = string.Format("操作失败!\r\n错误代码:{0}  错误描述:{1}", modelSql.RStatuCode, modelSql.RStatuDesc);
//            }
//            else
//            {
//                strErrInfo = modelSql.RStatuDesc == null ? string.Empty : modelSql.RStatuDesc.Replace("null", "").Trim();
//                if (!string.IsNullOrEmpty(strErrInfo)) return false;
//                bResult = true;
//            }
//            return bResult;
//        }

    }
}
